package mysql

import (
	"e_mall_server/models"
	"go.uber.org/zap"
)

/**
 * @Author: zhaobin
 * @Description 操作订单相关表
 * @Date: 2022-05-20 11:46
 */

// GetOrderListByUserId 通过用户id获取订单列表接口
func GetOrderListByUserId(userId int64) (orderList []models.Order, err error) {
	err = db.Preload("OrderItem").Where("user_id = ?", userId).Find(&orderList).Error
	zap.L().Debug("mysql.GetOrderByOrderId()", zap.Any("订单信息", orderList))
	return
}

// GetOrderList 获取订单列表
func GetOrderList(orderPageParam *models.OrderPageParam) (orderList []models.Order, err error) {
	num := orderPageParam.PageNum
	size := orderPageParam.PageSize
	err = db.Preload("OrderItem").Offset((num - 1) * size).Limit(size).Find(&orderList).Error
	zap.L().Debug("mysql.GetOrderList()", zap.Any("订单信息", orderList))
	return
}

// GetOrderCount 获取符合条件的订单数量
func GetOrderCount(orderPageParam *models.OrderPageParam) int64 {
	var count int64
	db.Model(&models.Order{}).Count(&count)
	return count
}

// AddOrder 创建订单
func AddOrder(order *models.Order) error {
	zap.L().Debug("mysql.AddOrder()", zap.Any("订单信息", order))
	// 进行数据库添加操作
	return db.Create(order).Error
}

// UpdateOrder 修改订单信息
func UpdateOrder(Order *models.Order) int64 {
	// 进行数据库修改操作
	return db.Debug().Model(&Order).Updates(&Order).RowsAffected
}

// DeleteOrderById 通过id删除订单数据
func DeleteOrderById(id int64) int64 {
	return db.Delete(&models.Order{Id: id}).RowsAffected
}

// GetOrderByOrderId 通过订单id获取订单详细信息
func GetOrderByOrderId(orderId string) (order models.Order, err error) {
	order.OrderId = orderId
	err = db.Preload("OrderItem").Where("order_id = ?", orderId).Find(&order).Error
	zap.L().Debug("mysql.GetOrderByOrderId()", zap.Any("订单信息", order))
	return
}

// GetOrderDetailById 通过id获取订单详细信息
func GetOrderDetailById(id int64) (order models.Order, err error) {
	err = db.Preload("OrderItem").Where("id = ?", id).Find(&order).Error
	zap.L().Debug("mysql.GetOrderByOrderId()", zap.Any("订单信息", order))
	return
}
